/**
 * Filename: SwitchTitleActivity.java
 *
 * @author Firefly
 * @contact postfirefly@163.com
 * @time 2016/5/7 22:29
 */
package com.github.postsanf.yinian.activity;

import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import com.github.postsanf.yinian.BaseActivity;
import com.github.postsanf.yinian.BaseApplication;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.constants.CommonConstants;
import com.github.postsanf.yinian.constants.ISkinUpdate;
import com.github.postsanf.yinian.utils.SkinPackageManager;
import com.umeng.analytics.MobclickAgent;

public class SwitchTitleActivity extends BaseActivity implements ISkinUpdate, View.OnClickListener {
    private Toolbar mToolbar;
    private EditText et_switch_title;
    private ImageView iv_switch_title_delete;
    private String mName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.yn_activity_switch_title);
        BaseApplication.getInstance().mActivitys.add(this);
        mName = getIntent().getStringExtra(CommonConstants.YNDATAS);
        initView();
    }

    private void initView() {
        mToolbar = (Toolbar) findViewById(R.id.switch_title_toolbar);
        setSupportActionBar(mToolbar);
        mToolbar.setNavigationIcon(R.drawable.ic_return);
        et_switch_title = (EditText) findViewById(R.id.et_switch_title);
        iv_switch_title_delete = (ImageView) findViewById(R.id.iv_switch_title_delete);
        et_switch_title.setText(mName);
        iv_switch_title_delete.setOnClickListener(this);
        et_switch_title.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() <= 0) {
                    iv_switch_title_delete.setVisibility(View.GONE);
                } else {
                    iv_switch_title_delete.setVisibility(View.VISIBLE);
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_switch_title_delete:
                et_switch_title.setText("");
                break;
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_sure, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                return true;
            case R.id.action_sure:
                String name = et_switch_title.getText().toString().trim();
                Intent intent = new Intent();
                intent.putExtra(CommonConstants.REQ_CODE, 1);
                intent.putExtra(CommonConstants.REQ_DATA, name);
                setResult(RESULT_OK, intent);
                finish();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void updateTheme() {
        if (mToolbar != null) {
            try {
                Resources mResource = SkinPackageManager.getInstance(this).mResources;
                mToolbar.setNavigationIcon(mResource.getDrawable(mResource.getIdentifier("ic_return", "drawable", "com.github.postsanf.skinby")));
                mToolbar.setBackgroundColor(mResource.getColor(mResource.getIdentifier("yncolor", "color", "com.github.postsanf.skinby")));
                mToolbar.setTitleTextColor(mResource.getColor(mResource.getIdentifier("border_color", "color", "com.github.postsanf.skinby")));

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        BaseApplication.getInstance().mActivitys.remove(this);
    }

    // 友盟统计
    public void onResume() {
        super.onResume();
        if (SkinPackageManager.getInstance(this).mResources != null) {
            updateTheme();
        }
        MobclickAgent.onPageStart(this.getClass().getSimpleName());
        MobclickAgent.onResume(this);
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(this.getClass().getSimpleName());
        MobclickAgent.onPause(this);
    }
}
